package com.wshl.aijiuunion;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.wshl.aijiuunion.controller.fragment.HomeFragment;
import com.wshl.aijiuunion.controller.fragment.MineFragment;
import com.wshl.aijiuunion.controller.fragment.ShopFragment;
import com.wshl.aijiuunion.controller.fragment.TabFragment;
import com.wshl.aijiuunion.controller.fragment.VideoFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.frame_main_content)
    FrameLayout frameMainContent;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.tv_shop)
    TextView tvShop;
    @BindView(R.id.tv_video)
    TextView tvVideo;
    @BindView(R.id.tv_mine)
    TextView tvMine;


    private TabFragment homeFragment;
    private TabFragment shopFragment;
    private TabFragment videoFragment;
    private TabFragment mineFragment;
    private TabFragment currentFragment;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragments();

    }

    private void initFragments() {

        fragmentManager = getSupportFragmentManager();
        homeFragment = new HomeFragment();
        mineFragment = new MineFragment();
        shopFragment = new ShopFragment();
        videoFragment = new VideoFragment();

        //开启一个事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //初始状态时,默认选中首页
        transaction.add(R.id.frame_main_content, homeFragment).commit();
        currentFragment = homeFragment;
        //设置选中状态
        tvHome.setSelected(true);
    }


    @OnClick({R.id.tv_home, R.id.tv_shop, R.id.tv_video, R.id.tv_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_home:
                resetBotom(1);
                break;
            case R.id.tv_shop:
                resetBotom(2);
                break;
            case R.id.tv_video:
                resetBotom(3);
                break;
            case R.id.tv_mine:
                resetBotom(4);
                break;
        }
    }


    //切换fragment
    private void switchFragment(TabFragment tofragment) {

        if (currentFragment != tofragment) {

            FragmentTransaction transaction = fragmentManager.beginTransaction();

            if (!tofragment.isAdded()) {

                transaction.hide(currentFragment).add(R.id.frame_main_content, tofragment).commit();

            } else {

                transaction.hide(currentFragment).show(tofragment).commit();
            }

            currentFragment = tofragment;
        }

    }


    private long mExitTime;// 上一次点击返回键的时间

    // 重写onKeyDown方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {  // 是返回键

            if (currentFragment != null
                    && currentFragment instanceof HomeFragment) {

                //如果当前显示的fragment是首页
                // 点击两次返回键退出程序
                if (System.currentTimeMillis() - mExitTime > 2000) {
                    // 如果当前点击返回键的时间减去上一次点击返回键的时间大于2秒
                    ToastUtils.showShort("再按一次退出");
                    // 重新赋值,方便下次使用
                    mExitTime = System.currentTimeMillis();
                } else {
                    // 如果小于两秒,就直接退出程序
                    SPUtils.getInstance().put(GlobConstant.ISSTARTMIAN, true);
                    finish();

                }
            } else {
                //如果当前显示的fragment不是首页，就先切换到首页
                resetBotom(1);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    private void resetBotom(int flag) {

        tvHome.setSelected(false);
        tvVideo.setSelected(false);
        tvShop.setSelected(false);
        tvMine.setSelected(false);

        switch (flag) {
            case 1:
                tvHome.setSelected(true);
                switchFragment(homeFragment);
                break;
            case 2:
                tvShop.setSelected(true);
                switchFragment(shopFragment);
                break;
            case 3:
                tvVideo.setSelected(true);
                switchFragment(videoFragment);
                break;
            case 4:
                tvMine.setSelected(true);
                switchFragment(mineFragment);
                break;
        }

    }


}
